package com.liner;

import javax.swing.*;
import java.awt.*;

/**
 * @author 一条淡水鱼 ゆ
 * @Classname Element
 * @Description 元素类
 * @Date 2023-04-02
 */
public abstract class Element {
    private int x, y;   //x, y 坐标
    private boolean selected; //是否选中
    private boolean eliminated; //是否可消除
    private int eliminatedIndex; //爆炸动画图片起始下标

    public Element(int x, int y) {
        this.x = x;
        this.y = y;
        this.selected = false;
        this.eliminated = false;
        this.eliminatedIndex = 0;
    }

    /**
     * 获取图片
     *
     * @return
     */
    public abstract ImageIcon getImage();


    public void paintElement(Graphics g) {
        if (isSelected()) {
            g.setColor(Color.GREEN);
            g.fillRect(x, y, World.ELEMENT_SIZE, World.ELEMENT_SIZE);
            this.getImage().paintIcon(null, g, this.x, this.y);

        } else if (isEliminated()) {
            //若没到最后一张爆炸图
            if (eliminatedIndex < Images.bombs.length) {
                Images.bombs[eliminatedIndex++].paintIcon(null, g, x, y);
            }

        } else {
            this.getImage().paintIcon(null, g, this.x, this.y);
        }
    }


    /**
     * 是否选中
     *
     * @return
     */
    boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    /**
     * 是否消除
     *
     * @return
     */
    boolean isEliminated() {
        return eliminated;
    }

    public void setEliminated(boolean eliminated) {
        this.eliminated = eliminated;
    }


    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
}
